<!DOCTYPE html>
<html lang="en">
<head>
  {% load static %}
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static './layui/css/layui.css' %}">
    <script src="{% static './layui/layui.js' %}"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <form class="layui-form" action="">
            <div class="layui-form-item">
                  <label class="layui-form-label">用户名</label>
                  <div class="layui-input-inline">
                    <input type="text" name="userName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" value="{{userName}}">
                  </div>
            </div>
            <div class="layui-form-item">
                  <label class="layui-form-label">昵称</label>
                  <div class="layui-input-inline">
                    <input type="text" name="nickName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" value="{{nickName}}">
                  </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">密码</label>
              <div class="layui-input-inline">
                <input type="password" name="password" id="pwd1" lay-verify="pass|required" placeholder="请输入密码" autocomplete="off" class="layui-input" value="{{password}}">
              </div>
              <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>

            <div class="layui-form-item">
                  <label class="layui-form-label">确认密码</label>
                  <div class="layui-input-inline">
                    <input type="password" name="password" id="pwd2" lay-verify="re_pass|required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                  </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                  <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input" value="{{email}}">
                </div>
              </div>

              <div class="layui-inline">
                <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                          <input type="text" name="phoneNumber" lay-verify="phone" placeholder="请输入" autocomplete="off" class="layui-input" value="{{phoneNumber}}">
                        </div>
              </div>
            </div>

            <div class="layui-form-item">

              <div class="layui-inline">
                <label class="layui-form-label">生日</label>
                <div class="layui-input-inline">
                  <input type="text" name="birthday" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input" value="{{birthday}}">
                </div>
              </div>

            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                  <input type="radio" name="sex" value="1" title="男" {% if sex == 1 %} checked="" {% endif %}>
                  <input type="radio" name="sex" value="2" title="女" {% if sex == 2 %} checked="" {% endif %}>
                </div>
            </div>

            <div class="layui-form-item">
              <label class="layui-form-label">地址</label>
              <div class="layui-input-block">
                <input type="text" name="address" lay-verify="required" autocomplete="off" placeholder="请输入标题" class="layui-input" value="{{address}}">
              </div>
            </div>

            <input type="hidden" name="user_id" value="{{id}}">

            <div class="layui-form-item">
                <div class="layui-input-block">
                  <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
    </form>
</body>
<script>
layui.use(['form', 'laydate'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,laydate = layui.laydate;
    //自定义验证规则
  form.verify({
    password: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位，且不能出现空格'
    ],
    phone: [
      /^[0-9]{11}$/
      ,'手机号必须是11位数字'
    ],
    re_pass: function (value){
        if($('#pwd1').val() !== value){
            return '两次密码不一致';
        }
    }
  });

  laydate.render({
    elem: '#date'
  });
  //监听提交
  form.on('submit(demo1)', function(data){
      // console.log(JSON.stringify(data.field))
      // debugger
      var userName = data.field['userName']
      var nickName = data.field['nickName']
      var password = data.field['password']
      var email = data.field['email']
      var phoneNumber = data.field['phoneNumber']
      var birthday = data.field['birthday']
      var sex = data.field['sex']
      var address = data.field['address']
      var user_id = data.field['user_id']

      $.ajax({
        method: "POST",
        url: "{%url 'api:user_edit' %}",
        data: { userName: userName,
        nickName: nickName,
        password: password,
        email: email,
        phoneNumber: phoneNumber,
        birthday: birthday,
        sex: sex,
        address: address,
        user_id:user_id
        },
        success: function (xxx) {
            if(xxx['code'] == 0){
                layer.msg(xxx['msg']);
                parent.layer.closeAll()
                // table关闭了
                parent.location.reload();
            }else{
                layer.msg(xxx['msg']);
            }
        }
      });
      return false;
  });
});
</script>
</html>